<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="单据编号">
              <j-input placeholder="请输入单据编号" v-model="queryParam.billNo"></j-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="是否付款">
              <j-dict-select-one-tag placeholder="请选择是否付款" v-model="queryParam.isPay" dictCode="is_pay"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="申请单位">
              <select-Company-input  v-model="queryParam.applicant" />
            </a-form-item>
          </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="开始/结束时间">
              <a-range-picker
                v-model="createTimeRange"
                format="YYYY-MM-DD"
                :placeholder="['开始时间', '结束时间']"
                @change="onCreateTimeChange" />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="关联项目">
                <select-project-input  v-model="queryParam.projectId" placeholder="请输入选择项目"  @input="selectProject" />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="项目归属年度">
                <j-dict-select-tag
                  type="list"
                  v-model="queryParam.projectYear"
                  dictCode="yjgsnd"
                  placeholder="请选择项目归属年度"
                />
              </a-form-item>
            </a-col>
          <!-- <template v-if="toggleSearchStatus"></template> -->
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="createTimeRange=[],searchReset()" icon="reload" style="margin-left: 8px">重置</a-button>
              <!-- <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a> -->
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" icon="download" @click="handleExportXls('工程进度款支付清单')">导出</a-button>
      <a-popover title="自定义列" trigger="click" placement="leftBottom">
        <template slot="content">
          <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
            <a-row>
              <template v-for="(item,index) in defColumns">
                <template v-if="item.key!='rowIndex'&& item.dataIndex!='action'">
                  <a-col :span="12" :key="index"><a-checkbox :value="item.dataIndex">{{ item.title }}</a-checkbox></a-col>
                </template>
              </template>
            </a-row>
          </a-checkbox-group>
        </template>
        <a style="float: right;"><a-icon type="setting" :style='iconStyle'/></a>
      </a-popover>
    </div>
    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-top: 25px;margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{x:scrollX}"
        :components="draggableComponent"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        :customRow="customRow"
        @change="handleTableChange">
        <a slot="projectId" slot-scope="text, record" @click="onDetail(record.projectId)">{{ text }}</a>
        <a slot="billNo" slot-scope="text, record" @click="getDetail(record.id)">{{ text }}</a>
        <span slot="notify" slot-scope="text, record">
          <a-icon title="催办提醒" v-if="record.taskUrge" theme="twoTone" twoToneColor="#eb2f96"
                  @click="taskNotifyMe(flowCode,record.id)" type="notification"/>
          {{ text }}
        </span>
        <template slot="status" slot-scope="status">
          <a-badge :status="status | statusTypeFilter" :text="status | statusFilter"/>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <span slot="action" slot-scope="text, record">
            <template v-if="record.bpmStatus === '1'">
            <a-button
                    class="ruleButton"
                    :ghost="true"
                    type="primary"
                    icon="edit"
                    @click="handleEdit(record)"
                    :disabled="isDisabledAuth('erpFinanceProjectProgressPaymentApplicationBill:edit')"
            />
            <a-divider type="vertical"/>
            <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                <a-button
                        class="ruleButton"
                        :ghost="true"
                        type="primary"
                        icon="delete"
                        :disabled="isDisabledAuth('erpFinanceProjectProgressPaymentApplicationBill:delete')"/>
            </a-popconfirm>
            <a-divider type="vertical"/>
            <a-button type="primary" size="small" @click="startProcess(record)" :disabled="isDisabledAuth('erpFinanceProjectProgressPaymentApplicationBill:startProcess')">发起流程</a-button>
        </template>
        <template v-else-if="showBtn(record.bpmStatus)&&queryParam.bizTaskType=='1'">
            <a-button type="primary" size="small" @click="handleProcess(record)">办理</a-button>
            <a-divider type="vertical"/>
        </template>
        <template v-else-if="queryParam.bizTaskType=='2' && showBtn(record.bpmStatus)">
            <a-button type="primary" size="small" @click="taskNotify(flowCode,record.id)">催办</a-button>
            <a-divider type="vertical"/>
            <a-popconfirm title="确定要作废吗?" @confirm="() => invalidProcess(record)">
                <a-button type="primary" size="small" :disabled="isDisabledAuth('erpFinanceProjectProgressPaymentApplicationBill:invalidProcess')">
                    作废流程
                </a-button>
            </a-popconfirm>
            <a-divider type="vertical"/>
        </template>
        <template v-if="record.checkStatus == '2'">
          <template v-if="record.isPay == '0'">
            <a-popconfirm title="确定付款吗?" @confirm="() => handlePay(record)" :disabled="isDisabledAuth('erpFinanceProjectProgressPaymentApplicationBill:pay')">
              <a-button type="primary" size="small"  :disabled="isDisabledAuth('erpFinanceProjectProgressPaymentApplicationBill:pay')">付款</a-button>
            </a-popconfirm>
            <a-divider type="vertical"/>
          </template>
        </template>
        <template v-if="record.bpmStatus !== '1'">
            <a-button type="primary" size="small" @click="handleTrack(record)" :disabled="isDisabledAuth('erpFinanceProjectProgressPaymentApplicationBill:viewTrack')">审批进度</a-button>
        </template>
        </span>
      </a-table>
    </div>
    <erp-finance-project-progress-payment-application-bill-modal ref="modalForm" @ok="modalFormOk"></erp-finance-project-progress-payment-application-bill-modal>
    <bpm-biz-task-deal-modal ref="taskDealModal" :path="path" :formData="formData" @ok="handleOk"></bpm-biz-task-deal-modal>
    <bpm-process-track-modal ref="bpmProcessTrackModal"></bpm-process-track-modal>
    <biz-task-notify-modal ref="taskNotifyModal"></biz-task-notify-modal>
    <biz-task-notify-me-modal ref="taskNotifyMeModal"></biz-task-notify-me-modal>
    <!-- 付款弹出框 -->
    <a-modal v-model="payFlag" title="付款" @ok="handlePayOk" :bodyStyle ="{
        'height':'200px',
        'display':'flex',
        'justifyContent': 'center',
        'alignItems': 'center'
      }">
      <j-form-container >
        <a-form-model ref="form" :model="payModel" :rules="validatorRules" slot="detail">
          <a-row :gutter="48">
            <a-col :span="12">
              <a-form-model-item label="是否已付款" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isPay">
                <j-dict-select-tag  type="list" v-model="payModel.isPay" dictCode="is_pay" placeholder="请选择是否已付款" />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="已付款金额(元)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="payAmount">
                <a-input-number :max="99999999" :min="0"  v-model="payModel.payAmount" placeholder="请输入已付款金额" style="width: 100%" disabled />
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="付款人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="payHandlerId">
                <select-user-by-dep :multi="false"
                  v-model="payModel.payHandlerId" disabled></select-user-by-dep>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="付款时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="payTime">
                <j-date placeholder="请输入付款时间" v-model="payModel.payTime" :show-time="true"
                      date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" disabled/>
              </a-form-model-item>
            </a-col>
            <a-input v-model="payModel.id" placeholder="id"  type="hidden"></a-input>
          </a-row>
        </a-form-model>
      </j-form-container>
    </a-modal>
    <!-- 项目详情开始 -->
    <erp-project-report-modal  ref="projectReport"></erp-project-report-modal>
    <!-- 项目详情结束 -->
  </a-card>
</template>
<script>
  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import {JeecgBpmListMixin} from '@/mixins/JeecgBpmListMixin'
  import BpmBizTaskDealModal from "@/views/modules/bpmbiz/common/BpmBizTaskDealModal.vue";
  import BpmProcessTrackModal from "@/views/modules/bpmbiz/common/BpmProcessTrackModal.vue";
  import BizTaskNotifyModal from "@/views/modules/bpmbiz/common/BizTaskNotifyModal.vue";
  import BizTaskNotifyMeModal from "@/views/modules/bpmbiz/common/BizTaskNotifyMeModal.vue";
  import ErpFinanceProjectProgressPaymentApplicationBillModal from './modules/ErpFinanceProjectProgressPaymentApplicationBillModal'
  import moment from 'moment'
  import { mapGetters } from 'vuex'
  import SelectPersonInput from '@/views/scy/components/SelectPersonInput'
  import { httpAction, getAction } from '@/api/manage'
  import {DisabledAuthFilterMixin} from '@/mixins/DisabledAuthFilterMixin'
  import SelectProjectInput from '@/views/scy/components/SelectProjectInput'
  import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
  import SelectCompanyInput from '@/views/scy/components/SelectCompanyInput'
  export default {
    name: 'ErpFinanceProjectProgressPaymentApplicationBillList',
    mixins:[JeecgBpmListMixin, mixinDevice, DisabledAuthFilterMixin],
    components: {
      ErpFinanceProjectProgressPaymentApplicationBillModal,
      BpmBizTaskDealModal,
      BpmProcessTrackModal,
      BizTaskNotifyModal,
      BizTaskNotifyMeModal,
      SelectPersonInput,
      SelectProjectInput,
      SelectUserByDep,
      SelectCompanyInput,
      ErpProjectReportModal:()=> import('@/views/erp/report/project/modules/ErpProjectReportModal')
    },
    data () {
      return {
        queryParam: {
          isPay:"0"
        },
        createTimeRange:[],
        description: '工程进度款支付申请单管理页面',
        payFlag:false,
        projectType:"2",
        validatorRules: {
          isPay:[{ required: true, message: '请选择是否付款!'}],
          payAmount:[{ required: true, message: '请输入金额!'}],
          payHandlerId:[{ required: true, message: '请选择联系人!'}],
          payTime:[{ required: true, message: '请选择支付时间!'}],
        },
        payModel:{},
        //表头
        columns:[],
        //列设置
        settingColumns:[],
        // 列定义
        defColumns: [
          {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'单据编号',
            align:"center",
            dataIndex: 'billNo',
            scopedSlots: { customRender: 'billNo' },
            width:130,
            ellipsis: true
          },
          {
            title:'申请人',
            align:"center",
            dataIndex: 'proposer_dictText',
            width:130,
            ellipsis: true
          },
          {
            title:'申请部门',
            align:"center",
            dataIndex: 'proposerDeptId_dictText',
            width:150,
            ellipsis: true,
          },
          {
            title:'关联项目',
            align:"center",
            dataIndex: 'projectId_dictText',
            width:130,
            scopedSlots: { customRender: 'projectId' },
            ellipsis: true,
          },
          {
            title:'项目归属年度',
            align:"center",
            dataIndex: 'projectYear_dictText',
            width:130,
            ellipsis: true
          },
          {
            title:'项目业务团队',
            align:"center",
            dataIndex: 'projectBusiTeamId_dictText',
            width:130,
            ellipsis: true
          },
          {
            title:'本项目累计支付金额（元）',
            align:"center",
            width:250,
            dataIndex: 'projectPaidTotalAmount',
            ellipsis: true
          },
          {
            title:'本项目累计支付比例%',
            align:"center",
            dataIndex: 'projectPaidRate',
            width:130,
            ellipsis: true
          },
          {
            title:'是否付款',
            align:"center",
            dataIndex: 'isPay_dictText',
            width:130,
            ellipsis: true
          },
          {
            title:'付款时间',
            align:"center",
            dataIndex: 'payTime',
            width:130,
            ellipsis: true
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            // fixed:"right",
            width:160,
            scopedSlots: {
            filterDropdown: 'filterDropdown',
            filterIcon: 'filterIcon',
            customRender: 'action' }
          }
        ],
        url: {
          queryProjectReportById: '/api/erp/project/report/erpProjectReport/queryById',
          list: "/api/erp/finance/projectProgress/erpFinanceProjectProgressPaymentApplicationBill/allList",
          delete: "/api/erp/finance/projectProgress/erpFinanceProjectProgressPaymentApplicationBill/delete",
          deleteBatch: "/api/erp/finance/projectProgress/erpFinanceProjectProgressPaymentApplicationBill/deleteBatch",
          exportXlsUrl: "/api/erp/finance/projectProgress/erpFinanceProjectProgressPaymentApplicationBill/inventoryExportXls",
          importExcelUrl: "api/erp/finance/projectProgress/erpFinanceProjectProgressPaymentApplicationBill/importExcel",
          bizCount: "/api/erp/finance/projectProgress/erpFinanceProjectProgressPaymentApplicationBill/bizCount",
          pay:"/api/erp/finance/projectProgress/erpFinanceProjectProgressPaymentApplicationBill/pay",//付款
        },
        //代码生成后需手动设置流程编码
        flowCode: 'onl_erp_finance_project_progress_payment_application_bill',
        formUrl: "/erp/finance/projectProgress/modules/ErpFinanceProjectProgressPaymentApplicationBillForm",
        formUrlMobile: "",
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
      this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      onDetail(id) {
        const that = this
        this.$nextTick(() => {
          getAction(this.url.queryProjectReportById, { id }).then((res) => {
            if (res.success) {
              const model = res.result
              that.$refs.projectReport.getDetail(model);
              that.$refs.projectReport.title = '项目一览表';
            }
          })
        })
      },
      searchReset(){
        this.queryParam={
          "isPay":"0"
        };
        this.loadData();
      },
       // table 双击查看详情
      customRow(record) {
        return {
          on: {
            // 事件
            dblclick: event => {
              this.$router.push({
                path: `ErpFinanceProjectProgressPaymentApplicationBillListDetail/${record.id}`
              })
            }
          }
        }
      },
      onCreateTimeChange(dateString) {
        this.queryParam.createTime_begin=moment(dateString[0]).format('YYYY-MM-DD');
        this.queryParam.createTime_end=moment(dateString[1]).format('YYYY-MM-DD');
      },
      //流程审批结束禁用
      getCheckboxProps(record) {
        console.log(record);
        return {
          props: {
            disabled: record.bpmStatus !=='1'
          }
        }
      },
       //关联项目
      selectProject(data){
        if(data){
          this.projectType=data.cutStatus;
        }
      },
      ...mapGetters(['userInfo']),
      //付款
      handlePay(record){
       //  this.validatorRules.isPay=[];
        this.$set(this.payModel, 'isPay', null);
        this.payFlag=true;
        this.$set(this.payModel, 'id', record.id);
        this.$set(this.payModel, 'payAmount', record.payAmount);
        this.$set(this.payModel, 'payTime', moment().format('YYYY-MM-DD HH:mm:ss'));
        this.$set(this.payModel, 'payHandlerId', this.userInfo().empId);
      },
      handlePayOk() {
       // this.validatorRules.isPay=[{ required: true, message: '请选择是否付款!'}];
         // 触发表单验证
        this.$refs.form.validate(valid => {
          if(valid){
            //确定支付
            if(this.payModel.isPay=="1"){
              httpAction(this.url.pay,this.payModel,'PUT').then((res)=>{
                if(res.success){
                  this.$message.success('付款成功！')
                  this.loadData(1);
                }
              })
            }
            this.payFlag = false;
          }
        })
      },
      //查看详情
      getDetail(id) {
        this.$router.push({
          path: `/erp/finance/projectProgress/ErpFinanceProjectProgressPaymentApplicationBillListDetail/${id}`,
        })
      },
      handleEdit: function (record) {
        this.$refs.modalForm.edit(record,true);
        this.$refs.modalForm.title = '编辑'
        this.$refs.modalForm.disableSubmit = false
      },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'handlerId',text:'经办人编号',dictCode:''})
        fieldList.push({type:'string',value:'handlerDeptId',text:'经办人部门ID',dictCode:''})
        fieldList.push({type:'string',value:'handlerDeptCode',text:'经办人部门编码',dictCode:''})
        fieldList.push({type:'string',value:'delFlag',text:'是否已删除',dictCode:''})
        fieldList.push({type:'string',value:'billNo',text:'单据编号',dictCode:''})
        fieldList.push({type:'string',value:'remark',text:'备注',dictCode:''})
        fieldList.push({type:'Text',value:'attachments',text:'附件',dictCode:''})
        fieldList.push({type:'string',value:'applicant',text:'申请单位',dictCode:''})
        fieldList.push({type:'string',value:'projectId',text:'关联项目',dictCode:''})
        fieldList.push({type:'string',value:'projectBusiTeamId',text:'项目业务团队',dictCode:''})
        fieldList.push({type:'string',value:'projectYear',text:'项目归属年度',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'projectPaidTotalAmount',text:'本项目累计支付金额',dictCode:''})
        fieldList.push({type:'BigDecimal',value:'projectPaidRate',text:'本项目累计支付比例',dictCode:''})
        fieldList.push({type:'string',value:'isPay',text:'是否付款',dictCode:'is_pay'})
        fieldList.push({type:'int',value:'checkStatus',text:'审核状态',dictCode:''})
        fieldList.push({type:'string',value:'bpmStatus',text:'流程状态',dictCode:'bpm_status'})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
